package com.abel.roommanager.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.abel.roommanager.pojo.Student;
import com.abel.roommanager.service.StudentService;
import com.abel.roommanager.util.Page;
import com.abel.roommanager.util.StudentRegexUtil;
@Controller
@RequestMapping("/stu")
@Scope("prototype")
public class StudentController {
	@Resource
	private StudentService studentService;
	@RequestMapping("/findAll")
	@ResponseBody
   public Page<Student> findAll(int curPage) {
		
		Page<Student> pages=studentService.findStudentsByCurPage(curPage);
	    return pages;
   }
	@RequestMapping("/del")
	@ResponseBody
	public int delStu(int id) {
		return studentService.delStu(id);	
	}
	@RequestMapping("/add")
	public ModelAndView addStu(Student stu,@RequestParam("file")MultipartFile file,HttpServletRequest request) throws ParseException, IllegalStateException, IOException {
		boolean isVal=StudentRegexUtil.validateStudent(stu);
		ModelAndView modelAndView=new ModelAndView("/jsp/addStu.jsp");
		if(!isVal) {
			modelAndView.addObject("res", "学生信息验证失败");
			return modelAndView;
		}
//	 获取上传的文件名
		if (file!=null) {
		String fileName=file.getOriginalFilename();
//	  获取文件存放路径
	 String path= request.getServletContext().getRealPath("/");
	 File file2=new File(path);
	path= file2.getParent()+"/image";
	File file3=new File(path);
	 if (!file3.exists()) {
		file3.mkdirs();
	}
//	新文件名，应该保存到数据库中
	 fileName=UUID.randomUUID().toString()+fileName;
//	 新路径
	 path=path+File.separator+fileName;
	 System.out.println(path);
	 file3=new File(path);
	 file.transferTo(file3);
	 stu.setHeadimage(fileName);
   }
//		获取当前系统时间
		Date date=new Date();
		SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		String livetime=sDateFormat.format(date);
		stu.setLivetime(livetime);
//		添加学生信息
		int rows=studentService.addStudent(stu);
		if (rows==1) {
			modelAndView.addObject("res", "学生信息添加成功");
		}else if(rows==0) {
			modelAndView.addObject("res", "学生信息添加失败");
		}else if (rows==2) {
			modelAndView.addObject("res", "对不起，该房间已住满！");
		}else if (rows==3) {
			modelAndView.addObject("res", "对不起，您的性别与当前宿舍类型不匹配！");
		}
		modelAndView.addObject("stu", stu);
		return modelAndView;
		
	}
	@RequestMapping("/findStuInfo")
	@ResponseBody
	public Student findStudentInfo(int id) {
		return studentService.findStudentById(id);
		
	}
	@RequestMapping("/updateRoom")
	@ResponseBody
	public int updateRoomBySid(Student stu) {
		return studentService.updateRoomBySid(stu);
	}
	@ResponseBody
	@RequestMapping("/findStudentByLimits")
	public Page<Student> findStudentByLimits(Student student,int curPage) {
		String sname=student.getSname();
		sname="%"+sname+"%";
		student.setSname(sname);
		Page<Student> students=studentService.findStudentsByLimits(student,curPage);
		return students;
		
	}
}
